<template>
  <div class="password">
    <ehl-form
      ref="form"
      :options="password.options"
      :data="password.data"
      custom-class="cpt_round_card password_box"
    >
    </ehl-form>
  </div>
</template>
<script>
  import Vue from 'vue'
  import { Component } from 'vue-property-decorator'
  import ehlForm from '@/common'
  import passwordConfig from '@/views/password.js'
  @Component({
    name: 'passwordView',
    components: { ehlForm },
    beforeRouteEnter(to, from, next) {
      $setStore('waiting', false)
      next()
    },
  })
  export default class passwordView extends Vue {
    password = new passwordConfig({
      // userName: '',
      password: '',
      newPassword: '',
      newPasswordagain: '',
    })
    formConfig = null
    mounted() {
      this.$refs.form.addParam({ vue: this })
      this.formConfig = this.$refs.form.getOptions()
    }
    goBack() {
      this.$router.go(-1)
    }
    async passwordClick() {
      try {
        if (this.password.data.password === this.password.data.newPassword) {
          $message.error('新老密码不能一样')
          return
        }
        if (this.password.data.newPassword !== this.password.data.newPasswordagain) {
          $message.error('再次输入的密码和新密码不一致')
          return
        }
        await this.$refs.form.validate()
        let res = await ajax.put('/v2/api/detection/user/changePassword', {
          // userName: this.password.data.userName,
          password: this.$md5(this.password.data.password),
          newPassword: this.$md5(this.password.data.newPassword),
        })
        $rReplace({ name: 'main' })
        this.$router.go(-1)
      } catch (error) {
        if (error === false) {
          console.log(error)
        } else {
          $message.error(error)
        }
      }
    }
  }
</script>
<style lang="less" scoped>
  .password {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: @cpt_grey_shadow;

    .password_box {
      width: 600px;
      padding: 80px 60px;

      .password {
        font-size: 14px;
        color: @cpt_main;
        text-align: right;
        cursor: pointer;
      }
    }
  }
</style>
